/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.latticeware.xtalk.app;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.HashSet;
import java.util.Observable;
import java.util.Observer;



/**
 *
 * @author sfisque
 */
public class ToolPalette
extends Frame
implements Observer
{    
    protected ToolButton pointerButton;
    
    protected ToolButton buttonButton;
    
    protected ToolButton fieldButton;
    
    protected ToolButton marqueeButton;
    
    protected ToolButton lassoButton;
    
    protected ToolButton pencilButton;
    
    protected ToolButton paintButton;
    
    protected ToolButton eraserButton;
    
    protected ToolButton lineButton;
    
    protected ToolButton sprayButton;
    
    protected ToolButton rectButton;
    
    protected ToolButton roundRectButton;
    
    protected ToolButton bucketButton;
    
    protected ToolButton ovalButton;
    
    protected ToolButton freehandButton;
    
    protected ToolButton textButton;
    
    protected ToolButton equiGonButton;
    
    protected ToolButton freeGonButton;
    
    protected GridBagConstraints gbc;
    
    protected HashSet<ToolButton> buttonSet;
    
    protected Point dragClick;
    
    protected Canvas dragBar;
    
    
    public ToolPalette(Frame owner) 
    throws InterruptedException
    {
        super();
        
        buttonSet = new HashSet<ToolButton>( 25);
       
        setAlwaysOnTop( true );
        setSize( 96, 202 );
        setResizable( false );
        setUndecorated( true );
        setBackground( Color.lightGray );
        setLayout( new GridBagLayout() );

        gbc = new GridBagConstraints();
        gbc.gridy = 0;
        gbc.gridx = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1;
        gbc.weighty = 0.3;
        gbc.gridheight = 1;
        gbc.gridwidth = 3;
        
        dragBar = new Canvas();
        dragBar.setPreferredSize( new Dimension( 96, 10 ) );
        dragBar.setBackground( Color.BLACK );
        dragBar.addMouseMotionListener( new PaletteDragAdapter() );
        dragBar.addMouseListener( new PaletteMouseAdapter() );
        ((GridBagLayout) getLayout()).setConstraints( dragBar, gbc );
        add( dragBar );
        
        gbc.gridwidth = 1;
        gbc.weighty = 1;
        gbc.gridheight = 3;
        gbc.gridy = 1;
        gbc.gridx = 0;
                        
        ImageFactory factory = ImageFactory.getInstance( owner );
        
        pointerButton = buildButton( factory.getPOINTER_IMG() );
        buttonButton = buildButton( factory.getBUTTON_IMG() );
        fieldButton = buildButton( factory.getFIELD_IMG() );
        marqueeButton = buildButton( factory.getMARQUEE_IMG() );
        lassoButton = buildButton( factory.getLASSO_IMG() );
        pencilButton = buildButton( factory.getPENCIL_IMG() );
        paintButton = buildButton( factory.getPAINTBRUSH_IMG() );
        eraserButton = buildButton( factory.getERASER_IMG() );
        lineButton = buildButton( factory.getLINE_IMG() );
        sprayButton = buildButton( factory.getSPRAYCAN_IMG() );
        rectButton = buildButton( factory.getRECTANGLE_IMG() );
        roundRectButton = buildButton( factory.getROUNDRECT_IMG() );
        bucketButton = buildButton( factory.getPAINTCAN_IMG() );
        ovalButton = buildButton( factory.getOVAL_IMG() );
        freehandButton = buildButton( factory.getFREEHAND_IMG() );
        textButton = buildButton( factory.getTEXT_IMG() );
        equiGonButton = buildButton( factory.getEQUIGON_IMG() );    
        freeGonButton = buildButton( factory.getPOLYGON_IMG() );
    }
    
    
    private ToolButton buildButton( Image image )
    {
        ToolButton which = new ToolButton();
        which.getMonitor().addObserver( this );
        which.setImage( image );
        ((GridBagLayout) getLayout()).setConstraints( which, gbc );
        add( which );
        buttonSet.add( which );
        
        gbc.gridx = ( gbc.gridx + 1 ) % 3;
        if( gbc.gridx == 0 )
        {
            gbc.gridy += 3;
        }

        return which;
    }


    @Override
    public void update(Observable o, Object arg)
    {
        System.out.println( "update:" + o );
        
        for( ToolButton tb : buttonSet )
        {
            if( ! tb.getMonitor().equals( o ) && tb.isToggleOn() )
            {
                tb.toggleOff();
            }
        }
        
        this.repaint();
    }
    
    
    class PaletteDragAdapter
    extends MouseMotionAdapter
    {
        @Override
        public void mouseDragged(MouseEvent e)
        {
            if( dragClick != null )
            {
                ToolPalette.this.setLocation( e.getXOnScreen() - dragClick.x, e.getYOnScreen() - dragClick.y );
            }
        }
        
    }
    
    
    class PaletteMouseAdapter
    extends MouseAdapter
    {
        @Override
        public void mousePressed(MouseEvent e)
        {
            dragClick = e.getPoint();
        }


        @Override
        public void mouseReleased(MouseEvent e)
        {
            dragClick = null;
        }
        
    }
}
